package com.fastwire.carriere.hibernate.daoimpl;

import java.util.List;
import java.util.Set;

import org.hibernate.FetchMode;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.fastwire.carriere.dao.DAOException;
import com.fastwire.carriere.dao.TopologicalLinkDao;
import com.fastwire.carriere.inventory.entity.TopologicalLink;

// TODO: Auto-generated Javadoc
/**
 * The Class TopologicalLinkDaoImpl.
 */
public class TopologicalLinkDaoImpl extends AbstractDAOImpl implements
		TopologicalLinkDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.dao.TopologicalLinkDao#findTLbyNEName(java.lang
	 * .String)
	 */
	@Override
	public List findTLbyNEName(String neID) throws DAOException {

		DetachedCriteria criteria = DetachedCriteria
				.forClass(TopologicalLink.class);

		criteria = criteria.add(getStatusCriteria());
		joinTp(criteria);
		Criterion aendCriterion = Restrictions.like("aendTp.name", neID,
				MatchMode.ANYWHERE);
		Criterion zendCriterion = Restrictions.like("zendTp.name", neID,
				MatchMode.ANYWHERE);

		criteria.add(Restrictions.or(aendCriterion, zendCriterion));

		return getHibernateTemplate()
				.findObjsByCriteria(criteria, false, false);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.dao.TopologicalLinkDao#saveOrUpdateTL(com.fastwire
	 * .carriere.inventory.entity.TopologicalLink)
	 */
	@Override
	public void saveOrUpdateTL(TopologicalLink topologicalLink)
			throws DAOException {
		super.saveOrUpdate(topologicalLink);

	}

	/**
	 * Join tp.
	 * 
	 * @param criteria
	 *            the criteria
	 */
	private void joinTp(DetachedCriteria criteria) {
		criteria.setFetchMode("aendTp", FetchMode.JOIN)
				.createAlias("aendTp", "aendTp")
				.setFetchMode("zendTp", FetchMode.JOIN)
				.createAlias("zendTp", "zendTp");
	}

	@Override
	public void invalidateAll() throws DAOException {
		DetachedCriteria criteria = DetachedCriteria
				.forClass(TopologicalLink.class);
		criteria = criteria.add(getStatusCriteria());

		getHibernateTemplate().invalidateObjsByCriteria(criteria);

	}

	@Override
	public List findTLbyNESet(Set<String> neIDs) throws DAOException {

		DetachedCriteria criteria = DetachedCriteria
				.forClass(TopologicalLink.class);

		criteria = criteria.add(getStatusCriteria());
		joinTp(criteria);

		Criterion jointCriterion = null;

		if (!neIDs.isEmpty()) {
			for (String neID : neIDs) {
				Criterion aendCriterion = Restrictions.like("aendTp.name",
						neID, MatchMode.ANYWHERE);
				Criterion zendCriterion = Restrictions.like("zendTp.name",
						neID, MatchMode.ANYWHERE);

				if (jointCriterion == null)

					jointCriterion = Restrictions.or(aendCriterion,
							zendCriterion);
				else {
					jointCriterion = Restrictions.or(jointCriterion,
							Restrictions.or(aendCriterion, zendCriterion));
				}
			}

			criteria.add(jointCriterion);
		}

		return getHibernateTemplate()
				.findObjsByCriteria(criteria, false, false);

	}

	@Override
	public List<TopologicalLink> getTopologicalLinksbyNameSet(
			List<String> tlNames) throws DAOException {
		DetachedCriteria criteria = DetachedCriteria
				.forClass(TopologicalLink.class);

		criteria = criteria.add(getStatusCriteria());

		criteria.add(Restrictions.in("emsNativeName", tlNames));
		criteria.add(Restrictions.eq("type", "L2"));

		return getHibernateTemplate().findObjsByCriteria(criteria, false, true);
	}
}
